<?php

namespace app\api\Service;

use app\common\library\Auth;
use app\common\model\Area;
use think\Config;
use think\Db;
use think\Validate;
use addons\epay\library\Service;

class CarDealerService
{
    // 品牌列表
    public static function getBrandList($data)
    {
        $first = isset($data['first']) ? $data['first'] : '';
        $where = [];
        if($first) $where['first'] = $first;

        $list = \app\admin\model\CarBrand::where($where)->field('id,name,image')->order('first asc,id desc')->select();
        return ['code' =>1,'msg'=>'查询成功','data'=>['total' =>count($list),'rows'=>$list]];
    }

    // 车系列表
    public static function getSeriesList($data)
    {
        $brand_id = isset($data['brand_id']) ? $data['brand_id'] : 0;

        $list = \app\admin\model\CarSeries::where(['brand_id'=>$brand_id])->field('id,name')->order('id desc')->select();
        return ['code' =>1,'msg'=>'查询成功','data'=>['total' =>count($list),'rows'=>$list]];
    }

    // 车辆型号
    public static function getModelList($data)
    {
        $series_id = isset($data['series_id']) ? $data['series_id'] : 0;

        $list = \app\admin\model\CarModel::where(['series_id'=>$series_id])->field('id,name')->order('id desc')->select();
        return ['code' =>1,'msg'=>'查询成功','data'=>['total' =>count($list),'rows'=>$list]];
    }

    // 车辆类别
    public static function getCategoryList()
    {
        $list = Config::get("site.car_category");
        return ['code' =>1,'msg'=>'查询成功','data'=>['total' =>count($list),'rows'=>$list]];
    }

    // 我要卖车
    public static function sellCar($data)
    {
        $user_id = Auth::instance()->id;


        $validate = new Validate([
            'brand_id'                  => 'require|number',
            'series_id'                 => 'require|number',
            'model_id'                  => 'require|number',
            'category'                  => 'require|number',
            'licensing_time'            => 'require',
            'kilometer'                 => 'require|number',
            'is_fire'                   => 'require|number',
            'is_blisters'               => 'require|number',
            'is_incident'               => 'require|number',
            'province_id'               => 'require|number',
            'city_id'                   => 'require|number',
            'area_id'                   => 'require|number',
            'lng'                       => 'require',
            'lat'                       => 'require',
            'name'                      => 'require',
            'mobile'                    => 'require|regex:^1\d{10}$',
            'exterior'                  => 'require',
            'glass'                     => 'require',
            'lights'                    => 'require',
            'wheel'                     => 'require',
            'interior'                  => 'require',
            'internal_function'         => 'require',
            'nacelle'                   => 'require',
            'motor'                     => 'require',
            'gearbox'                   => 'require',
        ]);

        $result = $validate->check($data);

        // 开始事务处理
        try{
            $data['user_id'] = $user_id;

            $result = \app\admin\model\CarOrder::create($data);

            // 提交事务
            Db::commit();
            // 判断订单和安装订单是否创建成功
            if($result){
                // 成功时返回成功信息和订单ID
                return ['code'=>1,'msg'=>'操作成功','data'=>''];
            }else{
                // 失败时返回失败信息
                return ['code'=>0,'msg'=>'操作失败','data'=>''];
            }
        }catch(\Exception $e){
            // 回滚事务
            Db::rollback();
            // 异常时返回异常信息
            return ['code'=>0,'message'=>$e->getMessage(),'data'=>''];
        }
    }

    // 售车记录
    public static function sellRecord($data)
    {
        // 获取当前用户认证信息
        $auth = Auth::instance();
        // 获取当前用户ID
        $user_id = $auth->id;

        $where = [];
        $where['user_id'] = $user_id;
        $limit = isset($data['limit']) && $data['limit'] ? $data['limit'] : 10;
        $status = isset($data['status']) && $data['status'] ? $data['status'] : -1;
        if($status == -1){
            $where['user_id'] = $user_id;
            $where['status'] = ['<>',100];
        }else{
            $where['user_id'] = $user_id;
            $where['status'] = $status;
        }

        $list = \app\admin\model\CarOrder::where($where)->field('id,brand_id,series_id,model_id,category,licensing_time,kilometer,city_id,exterior,status')->order('id desc')->paginate($limit)->each(function ($item){
            $item['brand_name'] = \app\admin\model\CarBrand::where(['id'=>$item['brand_id']])->value('name');
            $item['series_name'] = \app\admin\model\CarSeries::where(['id'=>$item['series_id']])->value('name');
            $item['model_name'] = \app\admin\model\CarModel::where(['id'=>$item['model_id']])->value('name');
            $item['city_name'] = Area::where(['id'=>$item['city_id']])->value('name');
            $exterior = explode(',',$item['exterior']);
            $item['exterior'] = $exterior[0];
        });
        return ['code' =>1,'msg'=>'查询成功','data'=>['total' =>$list->total(),'rows'=>$list]];
    }

    // 取消订单
    public static function cancelOrder($data)
    {
        // 获取当前用户认证信息
        $auth = Auth::instance();
        // 获取当前用户ID
        $user_id = $auth->id;
        $order_id = isset($data['order_id']) ? $data['order_id'] : 0;
        $order = \app\admin\model\CarOrder::where(['id'=>$order_id,'user_id'=>$user_id])->find();
        if(!$order) return ['code'=>0,'msg'=>'订单不存在'];

        try{
            $order->status = 2;
            $result = $order->save();

            // 提交事务
            Db::commit();
            // 判断订单和安装订单是否创建成功
            if($result){
                // 成功时返回成功信息和订单ID
                return ['code'=>1,'msg'=>'操作成功','data'=>''];
            }else{
                // 失败时返回失败信息
                return ['code'=>0,'msg'=>'操作失败','data'=>''];
            }
        }catch(\Exception $e){
            // 回滚事务
            Db::rollback();
            // 异常时返回异常信息
            return ['code'=>0,'message'=>$e->getMessage(),'data'=>''];
        }
    }

    // 已出售
    public static function alreadySold($data)
    {
        // 获取当前用户认证信息
        $auth = Auth::instance();
        // 获取当前用户ID
        $user_id = $auth->id;
        $order_id = isset($data['order_id']) ? $data['order_id'] : 0;
        $order = \app\admin\model\CarOrder::where(['id'=>$order_id,'user_id'=>$user_id])->find();
        if(!$order) return ['code'=>0,'msg'=>'订单不存在'];

        try{
            $order->status = 1;
            $result = $order->save();

            // 提交事务
            Db::commit();
            // 判断订单和安装订单是否创建成功
            if($result){
                // 成功时返回成功信息和订单ID
                return ['code'=>1,'msg'=>'操作成功','data'=>''];
            }else{
                // 失败时返回失败信息
                return ['code'=>0,'msg'=>'操作失败','data'=>''];
            }
        }catch(\Exception $e){
            // 回滚事务
            Db::rollback();
            // 异常时返回异常信息
            return ['code'=>0,'message'=>$e->getMessage(),'data'=>''];
        }
    }

    // 车商认证
    public static function carAuth($data)
    {
        // 获取当前用户认证信息
        $auth = Auth::instance();
        // 获取当前用户ID
        $user_id = $auth->id;

        $result = \app\admin\model\CarDealerCertification::where(['user_id'=>$user_id,'status'=>['in',[0,1]]])->order('id desc')->find();
        if($result && $result['status'] == 0) return ['code'=>0,'msg'=>'已提交认证，不要重复提交'];
        if($result && $result['status'] == 1) return ['code'=>0,'msg'=>'已认证成功，不要重复提交'];

        if (!$data['realname']) return ['code'=>0,'msg'=>'姓名不为空','data'=>''];
        if (!$data['idnumber'] || !Validate::regex($data['idnumber'], "^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$")) {
            return ['code'=>0,'msg'=>'身份证格式不正确','data'=>''];
        }
        if (!$data['legal_person']) return ['code'=>0,'msg'=>'法人不为空','data'=>''];
        if (!$data['mobile'] || !Validate::regex($data['mobile'], "^1\d{10}$")) {
            return ['code'=>0,'msg'=>'手机号格式不正确','data'=>''];
        }
        if (!$data['mail'] || !Validate::regex($data['mail'], "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$")) {
            return ['code'=>0,'msg'=>'邮箱格式不正确','data'=>''];
        }
        if (!$data['province_id']) return ['code'=>0,'msg'=>'请选择省','data'=>''];
        if (!$data['city_id']) return ['code'=>0,'msg'=>'请选择市','data'=>''];
        if (!$data['area_id']) return ['code'=>0,'msg'=>'请选择区','data'=>''];
        if (!$data['idcard_r']) return ['code'=>0,'msg'=>'请上传身份证人像','data'=>''];
        if (!$data['idcard_g']) return ['code'=>0,'msg'=>'请上传身份证国徽','data'=>''];
        if (!$data['business_license']) return ['code'=>0,'msg'=>'请上传营业执照','data'=>''];

        // 开始事务处理
        try{
            $data['user_id'] = $user_id;

            $result = \app\admin\model\CarDealerCertification::create($data);

            // 提交事务
            Db::commit();
            // 判断订单和安装订单是否创建成功
            if($result){
                // 成功时返回成功信息和订单ID
                return ['code'=>1,'msg'=>'操作成功','data'=>''];
            }else{
                // 失败时返回失败信息
                return ['code'=>0,'msg'=>'操作失败','data'=>''];
            }
        }catch(\Exception $e){
            // 回滚事务
            Db::rollback();
            // 异常时返回异常信息
            return ['code'=>0,'message'=>$e->getMessage(),'data'=>''];
        }
    }

    // 选择城市
    public static function selectCity($data)
    {
        // 查询所有城市数据
        $cities = Area::where('level', 2)
                      ->field('id, name, first')
                      ->order('first asc') // 按首字母排序
                      ->select();

        // 初始化结果数组
        $result = [];

        foreach ($cities as $city) {
            // 获取城市名称的第一个字母
            $firstLetter = $city['first'];

            // 如果结果数组中还没有这个字母作为键，则初始化
            if (!isset($result[$firstLetter])) {
                $result[$firstLetter] = [];
            }

            // 将城市添加到对应字母的数组中
            $result[$firstLetter][] = [
                'id' => $city['id'],
                'name' => $city['name']
            ];
        }

        // 按字母顺序排序结果数组
        ksort($result);

        return ['code' => 1, 'msg' => '查询成功', 'data' => $result];
    }

    // 选择品牌
    public static function selectBrand($data)
    {
        // 查询所有城市数据
        $cities = \app\admin\model\CarBrand::field('id, name, first')
                      ->order('first asc') // 按首字母排序
                      ->select();

        // 初始化结果数组
        $result = [];

        foreach ($cities as $city) {
            // 获取城市名称的第一个字母
            $firstLetter = $city['first'];

            // 如果结果数组中还没有这个字母作为键，则初始化
            if (!isset($result[$firstLetter])) {
                $result[$firstLetter] = [];
            }

            // 将城市添加到对应字母的数组中
            $result[$firstLetter][] = [
                'id' => $city['id'],
                'name' => $city['name']
            ];
        }

        // 按字母顺序排序结果数组
        ksort($result);

        return ['code' => 1, 'msg' => '查询成功', 'data' => $result];
    }

    // 距离筛选
    public static function distance($data)
    {
        $lat = isset($data['lat']) ? $data['lat'] : '';
        $lng = isset($data['lng']) ? $data['lng'] : '';
        $distance = isset($data['distance']) ? $data['distance'] : 3;


        $latitude = $lat;
        $longitude = $lng;

        //1.计算最大最小经纬度范围
        $range  = 180 / pi() * $distance / 6378.138; //搜索 N km 之内
        $lngR   = $range / cos($latitude * pi() / 180);
        $maxLat = $latitude + $range; //最大纬度
        $minLat = $latitude - $range; //最小纬度
        $maxLng = $longitude + $lngR; //最大经度
        $minLng = $longitude - $lngR; //最小经度
//        return ['maxLat' => $maxLat,'minLat' => $minLat,'maxLng' => $maxLng,'minLng' => $minLng];
        //2.查找经纬度符合条件的数据
        $list = \app\admin\model\CarOrder::where(['lat'=>['between',[$minLat,$maxLat]]])
            ->where(['lng'=>['between',[$minLng,$maxLng]]])
            ->where(['status'=>['<>',100]])
            ->field('id,brand_id,model_id,city_id,category,licensing_time,kilometer,exterior,status')
            ->select();
        if($list){
            foreach ($list as $k=>$v){
                $list[$k]['brand_name'] = \app\admin\model\CarBrand::where(['id'=>$v['brand_id']])->value('name');
                $list[$k]['model_name'] = \app\admin\model\CarModel::where(['id'=>$v['model_id']])->value('name');
                $list[$k]['city_name'] = \app\admin\model\Area::where(['id'=>$v['city_id']])->value('name');
                $exterior = explode(',',$v['exterior']);
                $list[$k]['exterior'] = $exterior[0];
            }
        }else{
            $list = [];
        }
        return ['code' =>1,'msg'=>'查询成功','data'=>['total' =>count($list),'rows'=>$list]];
    }

    // 车辆详情
    public static function carDetail($data)
    {
        $auth = Auth::instance();
        $user_id = $auth->id;

        $id = isset($data['order_id']) ? $data['order_id'] : '';
        $order = \app\admin\model\CarOrder::where(['id'=>$id])->find();
//        return $order;
        if(!$order) return ['code'=>0,'msg'=>'未找到相关订单'];
        $order['exterior'] = explode(',',$order['exterior']);
        $order['glass'] = explode(',',$order['glass']);
        $order['lights'] = explode(',',$order['lights']);
        $order['wheel'] = explode(',',$order['wheel']);
        $order['interior'] = explode(',',$order['interior']);
        $order['internal_function'] = explode(',',$order['internal_function']);
        $order['nacelle'] = explode(',',$order['nacelle']);
        $order['motor'] = explode(',',$order['motor']);
        $order['gearbox'] = explode(',',$order['gearbox']);

        $order['brand_name'] = \app\admin\model\CarBrand::where(['id'=>$order['brand_id']])->value('name');
        $order['series_name'] = \app\admin\model\CarSeries::where(['id'=>$order['series_id']])->value('name');
        $order['model_name'] = \app\admin\model\CarModel::where(['id'=>$order['model_id']])->value('name');

        // 自己的信息不隐藏
        if($order['user_id'] == $user_id){
            $order['province'] = \app\admin\model\Area::where(['id'=>$order['province_id']])->value('name');
            $order['city'] = \app\admin\model\Area::where(['id'=>$order['city_id']])->value('name');
            $order['area'] = \app\admin\model\Area::where(['id'=>$order['area_id']])->value('name');
            $order['city_name'] = $order['city'];
        }else{
            // 是否开通车商VIP
            $car_vip = \app\admin\model\UserCarView::where(['id'=>$auth->id])->find();
            $order['enquiry_price'] = Config::get('site.enquiry_price');
            if($car_vip){
                if($car_vip['viptime'] > time() || $car_vip['frequency'] > 0){
                    $order['province'] = \app\admin\model\Area::where(['id'=>$order['province_id']])->value('name');
                    $order['city'] = \app\admin\model\Area::where(['id'=>$order['city_id']])->value('name');
                    $order['area'] = \app\admin\model\Area::where(['id'=>$order['area_id']])->value('name');
                    $order['city_name'] = $order['city'];
                    $order['is_toview'] = 1;
                    // 查看次数-1
                    \app\admin\model\UserCarView::where(['id'=>$auth->id])->setDec('frequency');
                }else{
                    $order['name'] = '';
                    $order['mobile'] = '';
                    $order['province'] = '';
                    $order['city'] = '';
                    $order['area'] = '';
                    $order['is_toview'] = 0;
                }
            }else{
                $order['name'] = '';
                $order['mobile'] = '';
                $order['province'] = '';
                $order['city'] = '';
                $order['area'] = '';
                $order['is_toview'] = 0;
            }
        }

        return ['code' =>1,'msg'=>'查询成功','data'=>$order];
    }

    // 会员价格
    public static function memberPrice($data)
    {
        $list = Config::get("site.vip_price");
        $data = [];
        foreach($list as $k => $v){
            $data[] = ['days'=>$k,'price'=>$v];
        }
        return ['code' =>1,'msg'=>'查询成功','data'=>$data];
    }

    // 开通会员
    public static function openMember($data)
    {
        // 初始化认证类实例，用于获取用户信息
        $auth = Auth::instance();
        // 获取当前用户ID
        $user_id = $auth->id;
        // 获取当前用户的微信OpenID
        $openid = $auth->openid;
        // 检查是否绑定了微信OpenID
        if(!$openid) return ['code'=>0,'msg'=>'请先绑定微信','data'=>''];
        $type = $data['type'];
        $amount = $data['amount'];
        if($type == '1'){
            $number = ($amount / Config::get('site.enquiry_price'));
            if(!is_int($number))  return ['code'=>0,'msg'=>'金额错误','data'=>''];
        }
        if($type == '2'){
            $day = $data['day'];
            $vip_price = Config::get('site.vip_price');
            $price =  $vip_price[$day];
            if($price != $amount)  return ['code'=>0,'msg'=>'金额错误','data'=>''];
        }

        // 创建订单
        $params = [
            'type'      =>  $type,
            'order_no'      =>  getNewOrderNo(16),
            'user_id'      =>  $user_id,
            'amount'      =>  $amount,
            'payamount'      =>  $amount,
        ];


        $vipOrder_no = Db::name('user_car_vip')->insert($params);
        // 默认支付方式为小程序
        $method = 'miniapp';
        // 支付标题
        $title = "购买订单-{$params['order_no']}";

            $type = 'wechat';
            // 构建微信支付回调URL
            $notifyurl = request()->domain() . '/addons/epay/index/carvip_notifyx/paytype/wechat';

            // 构建支付请求参数
            $params = [
                'amount'    => $amount,
                'orderid'   => $params['order_no'],
                'type'      => 'wechat',
                'title'     => '【'.$title.'】',
                'method'    => $method,
                'openid'    => $openid,
                'notifyurl'    => $notifyurl,
            ];
            // 测试支付，固定金额为0.01元
            // $params['amount'] = 0.01;
            // 调用微信支付接口发起支付请求
            $response = Service::submitOrder($params);
            // 解析支付接口返回的响应
            $response = json_decode($response,true);
            return ['code'=>1,'msg'=>'操作成功','data'=>$response];
    }

    // 我的认证
    public static function myAuth($data)
    {
        $auth = Auth::instance();
        $user_id = $auth->id;
        $result = \app\admin\model\CarDealerCertification::where(['user_id'=>$user_id])->order('id desc')->find();

        if(!$result) return ['code'=>0,'msg'=>'未提交认证','data'=>''];
        $result['province'] = \app\admin\model\Area::where(['id'=>$result['province_id']])->value('name');
        $result['city'] = \app\admin\model\Area::where(['id'=>$result['city_id']])->value('name');
        $result['area'] = \app\admin\model\Area::where(['id'=>$result['area_id']])->value('name');
        $result['business_license'] = explode(',',$result['business_license']);

        if($result['status'] == 0) return ['code'=>1,'msg'=>'请等待工作人员审核','data'=>''];
        if($result['status'] == 1) return ['code'=>1,'msg'=>'可进入车商端','data'=>$result];
        if($result['status'] == 2) return ['code'=>1,'msg'=>'请重新提交认证','data'=>$result];
    }

    // 修i该认证信息
    public static function editAuth($data)
    {
        // 获取当前用户认证信息
        $auth = Auth::instance();
        // 获取当前用户ID
        $user_id = $auth->id;
        $id = $data['id'];

        $result = \app\admin\model\CarDealerCertification::where(['user_id'=>$user_id,'status'=>['in',[0,1]]])->order('id desc')->find();
        if($result['status'] == 0) return ['code'=>0,'msg'=>'已提交认证，不要重复提交'];
        if($result['status'] == 1) return ['code'=>0,'msg'=>'已认证成功，不要重复提交'];

        $validate = new Validate([
            'realname'                  => 'require|regex:^[\u4e00-\u9fa5]{2,5}$',
            'idnumber'                  => 'require|regex:^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$',
            'legal_person'              => 'require|regex:^[\u4e00-\u9fa5]{2,5}$',
            'mobile'                    => 'require|regex:^1\d{10}$',
            'mail'                          => 'require|regex:^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$',
            'province_id'               => 'require|number',
            'city_id'                   => 'require|number',
            'area_id'                   => 'require|number',
            'idcard_r'                  => 'require',
            'idcard_g'                  => 'require',
            'business_license'          => 'require',
        ]);

        $result = $validate->check($data);

        // 开始事务处理
        try{
            $data['user_id'] = $user_id;

            $result = \app\admin\model\CarDealerCertification::where('id',$id)->update($data);

            // 提交事务
            Db::commit();
            // 判断订单和安装订单是否创建成功
            if($result){
                // 成功时返回成功信息和订单ID
                return ['code'=>1,'msg'=>'操作成功','data'=>''];
            }else{
                // 失败时返回失败信息
                return ['code'=>0,'msg'=>'操作失败','data'=>''];
            }
        }catch(\Exception $e){
            // 回滚事务
            Db::rollback();
            // 异常时返回异常信息
            return ['code'=>0,'message'=>$e->getMessage(),'data'=>''];
        }
    }

    // 账单明细
    public static function billDetail($data)
    {
        $auth = Auth::instance();
        $user_id = $auth->id;

        $limit = isset($data['limit']) && $data['limit'] ? $data['limit'] : 10;

        $list = Db::name('user_car_vip')->where(['user_id'=>$user_id,'paid'=>1])->order('createtime desc')->paginate($limit)->each(function($item){
//            $item['type'] = $item['type'] == 1 ? '购买次数' : '购买会员';
        });
        return ['code'=>1,'msg'=>'操作成功','data'=>$list];
    }

    // 测试回调
    public static function testNotifyx($data)
    {

        $paytype = 'wechat';
        $data['return_code'] = 'SUCCESS';
        $data['result_code'] = 'SUCCESS';
        $data['transaction_id'] = $data['out_trade_no'];
        $data['total_fee'] = 1;
        $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;

        if($paytype == 'wechat' && $data['return_code'] == 'SUCCESS' && $data['result_code'] == 'SUCCESS'){
                $order = Db::name('user_car_vip')->where('order_no', $data['out_trade_no'])->find();
//            return $order;
                if(!$order){
                    return json(['code' => 'FAIL', 'message' => '订单不存在'], 500, ['Content-Type' => 'application/json']);
                }

                //判断是否已支付过
                if( $order['paid'] == '1' ){
                    return json(['code' => 'FAIL', 'message' => '订单已支付'], 500, ['Content-Type' => 'application/json']);
                }

                //修改订单状态
                Db::name('user_car_vip')->where('order_no', $data['out_trade_no'])->update(['trade_no'=>$data['transaction_id'],'paid'=>1,'paytime'=>time()]);

                $user_view = Db::name('user_car_view')->where(['user_id'=>$order['user_id']])->find();
                if($user_view){
                    if($order['type'] == 1){
                        $amount = $order['amount'];$enquiry_price = Config::get('site.enquiry_price');
                        $number = $amount / $enquiry_price;
                        Db::name('user_car_view')->where(['user_id'=>$order['user_id']])->setInc('frequency',$number);
                    }
                    if($order['type'] == 2){
                        $day = $order['day'];
                        $length = $day * 84600;
                        if($user_view['is_vip']){
                            $viptime = $user_view['viptime'] + $length;
                            Db::name('user_car_view')->where(['user_id'=>$order['user_id']])->setInc(['viptime'=>$viptime]);
                        }else{
                            $viptime = time() + $length;
                            Db::name('user_car_view')->where(['user_id'=>$order['user_id']])->update(['viptime'=>$viptime]);
                        }
                    }
                }else{
                    if($order['type'] == 1){
                        $amount = $order['amount'];$enquiry_price = Config::get('site.enquiry_price');
                        $number = $amount / $enquiry_price;
                        Db::name('user_car_view')->insert(['user_id'=>$order['user_id'],'frequency'=>$number]);
                    }
                    if($order['type'] == 2){
                        $day = $order['day'];
                        $length = $day * 84600;
                        $viptime = time() + $length;
                        Db::name('user_car_view')->insert(['user_id'=>$order['user_id'],'is_vip'=>1,'viptime'=>$viptime]);
                    }
                }

            }
    }
}